package com.longe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longe.common.Result;
import com.longe.dao.FriendMapper;
import com.longe.entity.Friend;
import com.longe.entity.User;
import com.longe.service.FriendService;
import com.longe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author longe
 * @since 2020-05-23
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {

    @Autowired
    private UserService userService;

    @Override
    public Result<List<User>> listFriendRequest(Integer userId) {

        QueryWrapper<Friend> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Friend> list = this.list(queryWrapper);

        Collection<Integer> friendIds = list.stream().mapToInt(Friend::getFriendId).distinct().boxed().collect(Collectors.toList());
        QueryWrapper<User> query = new QueryWrapper();
        query.in("id", friendIds).select("id", "username", "nickname", "pic_small");
        List<User> userList = userService.list(query);

        return new Result<>(true, "SUCCESS", userList);
    }
}
